<template>
  <div>
    <!-- 面包屑导航区域 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>订单管理</el-breadcrumb-item>
      <el-breadcrumb-item>订单列表</el-breadcrumb-item>
    </el-breadcrumb>

    <el-card class="box-card">
      <!-- 输入框位置 -->
      <el-row :gutter="10" type="flex" justify="space-between">
        <el-col :span="10">
          <el-input
            placeholder="请输入内容"
            clearable
            ref="input"
            @clear="getOrdersLise()"
            @keydown.enter.native="queryChange"
          >
            <el-button slot="append" icon="el-icon-search" @click="getOrdersLise()"></el-button>
          </el-input>
        </el-col>
      </el-row>
      <el-table :data="orderList" border style="width: 100%" height="calc(100vh - 250px)">
        <el-table-column align="center" type="index" label="序号"></el-table-column>
        <el-table-column align="center" prop="order_number" label="订单编号"></el-table-column>
        <el-table-column align="center" prop="order_price" label="订单价格"></el-table-column>
        <el-table-column align="center" prop="pay_status" label="是否付款"></el-table-column>
        <el-table-column align="center" prop="is_send" label="是否发货"></el-table-column>
        <el-table-column align="center" label="下单时间">
          <template v-slot="scoped">
            <span>{{scoped.row.create_time | TimeTransition}}</span>
          </template>
        </el-table-column>
        <el-table-column align="center" label="操作">
          <template v-slot="operation">
            <!-- 修改地址位置信息 -->
            <el-button
              size="mini"
              type="primary"
              icon="el-icon-edit"
              @click="showBox(operation.row)"
            ></el-button>
            <!-- 商品物流信息 -->
            <el-button
              size="mini"
              type="success"
              icon="el-icon-position"
              @click="showProgressBox(operation.row)"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页区域 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[1, 2, 5, 20]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      ></el-pagination>

      <!-- 修改位置的对话框 -->
      <el-dialog
        title="修改地址"
        :visible.sync="addressVisible"
        width="50%"
        @close="addressDialogClosed"
        center
      >
        <el-form
          :model="addressForm"
          :rules="addressFormRules"
          ref="addressFormRef"
          label-width="100px"
        >
          <el-form-item label="省市区/县" prop="address1">
            <el-cascader
              v-model="addressForm.address1"
              :options="cityData"
              :props="addressProps"
              style="width:350px"
              clearable
              filterable
            ></el-cascader>
          </el-form-item>
          <el-form-item label="详细地址" prop="address2">
            <el-input v-model="addressForm.address2"></el-input>
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="addressVisible = false">取 消</el-button>
          <el-button type="primary" @click="addressSubmit">确 定</el-button>
        </span>
      </el-dialog>

      <!-- 展示物流进度的对话框 -->
      <!-- 修改位置的对话框 -->
      <el-dialog
        title="物流展示"
        :visible.sync="progressBoxVisible"
        width="50%"
        @close="progressBoxDialogClosed"
        center
      >
        <el-timeline>
          <el-timeline-item
            v-for="(activity, index) in progressInfo"
            :key="index"
            icon="el-icon-circle-check"
            :type="activity.context"
            color="green"
            :timestamp="activity.time"
          >{{activity.context}}</el-timeline-item>
        </el-timeline>
        <span slot="footer" class="dialog-footer">
          <el-button @click="progressBoxVisible = false">取 消</el-button>
          <el-button type="primary" @click="addressSubmit">确 定</el-button>
        </span>
      </el-dialog>
    </el-card>
  </div>
</template>

<script>
import cityData from "./citydata.js";
export default {
  data: function() {
    var checkAddress1 = (rule, value, callback) => {
      console.log(value);
      if (value.length === 0) {
        return callback(new Error("省市区/县不能为空"));
      }
    };
    return {
      queryInfo: {
        //   查询参数
        query: "",
        // 当前页码
        pagenum: 1,
        // 每页显示条数
        pagesize: 5
      },
      total: 0,
      orderList: [],
      addressVisible: false,
      progressBoxVisible: false,
      addressForm: {
        address1: [],
        address2: ""
      },
      addressFormRules: {
        address1: [
          { required: true, message: "请选择省市区/县", trigger: "change" },
          {
            validator: checkAddress1,
            message: "请选择省市区/县",
            trigger: "expand-change"
          }
        ],
        address2: [
          { required: true, message: "请输入详细地址", trigger: "blur" }
        ]
      },
      cityData: cityData,
      addressProps: {
        label: "label",
        value: "value",
        children: "children",
        expandTrigger: "hover"
      },
      progressInfo: {}
    };
  },
  created: function() {
    this.getOrdersLise();
  },
  methods: {
    getOrdersLise: async function() {
      const { data: res } = await this.$http.get("/orders", {
        params: this.queryInfo
      });
      if (res.meta.status !== 200) {
        this.$message({
          showClose: true,
          message: "获取订单列表失败",
          type: "error"
        });
        return;
      }
      console.log(res);

      this.total = res.data.total;
      this.orderList = res.data.goods;
    },
    // 改变当前页触发
    handleCurrentChange: function(val) {
      this.queryInfo.pagenum = val;
      this.getOrdersLise();
    },
    // 改变当前显示多少条触发
    handleSizeChange: function(val) {
      this.queryInfo.pagesize = val;
      this.getOrdersLise();
    },
    showBox: function() {
      this.addressVisible = true;
    },
    // 改变修改位置对话框
    addressDialogClosed: function() {
      this.$refs.addressFormRef.resetFields();
    },
    //提交修改
    addressSubmit: function() {},
    // 物流对话框关闭
    progressBoxDialogClosed: function() {},
    showProgressBox: async function() {
      this.progressBoxVisible = true;
      const { data: res } = await this.$http.get("/kuaidi/1106975712662");
      if (res.meta.status !== 200) {
        return this.$message.error("获取物流失败!");
      }
      this.progressInfo = res.data;
      console.log(res);
    }
  },
  filters: {
    TimeTransition: function(timestamp) {
      var date = new Date(timestamp * 1000); //时间戳为10位需*1000，时间戳为13位的话不需乘1000
      var Y = date.getFullYear() + "-";
      var M =
        (date.getMonth() + 1 < 10
          ? "0" + (date.getMonth() + 1)
          : date.getMonth() + 1) + "-";
      var D =
        (date.getDate() < 10 ? "0" + date.getDate() : date.getDate()) + " ";
      var h =
        (date.getHours() < 10 ? "0" + date.getHours() : date.getHours()) + ":";
      var m =
        (date.getMinutes() < 10 ? "0" + date.getMinutes() : date.getMinutes()) +
        ":";
      var s =
        date.getSeconds() < 10 ? "0" + date.getSeconds() : date.getSeconds();
      return Y + M + D + h + m + s;
    }
  }
};
</script>
<style lang='less' scoped>
.el-pagination {
  margin-top: 10px;
}
.el-table {
  margin-top: 10px;
}
.el-cascader {
  width: 100% !important;
}
</style>